package com.longteng.diamond.service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.jayway.jsonpath.JsonPath;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.service.impl.XPathMatchImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * Created by taki
 * DATE : 2018/9/9 0009
 * TIME : 22:58
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:165157905@qq.com">taki</a>
 */
@Component
public class ExpressionCheckService {
    @Autowired
    XPathMatchImpl xPathMatch;
    public RequestResult check(String expressionJson,String expectResult,Integer matchType){
        RequestResult requestResult = new RequestResult();
        List list= Lists.newArrayList();
        List<String> expressionList = JSON.parseObject(expressionJson, List.class);
        switch (matchType){
            case 4:
                for (String expression : expressionList) {
                    try {
                        Object value = JsonPath.read(expectResult, expression);
                        list.add(value);
                        requestResult.setSuccess(true);
                    } catch (Exception e) {
                        requestResult.setSuccess(false);
                        requestResult.setMsg(e.getMessage());
                        return requestResult;
                    }
                }
                break;
            case 5:
                /**获取入参数据并且转换成document**/
                InputStream is = new ByteArrayInputStream(expectResult.getBytes());
                Document document=xPathMatch.getDocumentObj(is);
                /**定义一个xPath解析器**/
                XPath xPath= XPathFactory.newInstance().newXPath();
                for (String expression : expressionList) {
                    try {
                        Node node=(Node) xPath.evaluate(expression,document,XPathConstants.NODE);
                        if(null==node){
                            requestResult.setMsg(expression);
                            requestResult.setSuccess(false);
                            return requestResult;
                        }
                        Object value=node.getNodeValue();
                        list.add(value);
                        requestResult.setSuccess(true);
                    } catch (Exception e) {
                        requestResult.setSuccess(false);
                        requestResult.setMsg(e.getMessage());
                        return requestResult;
                    }
                }
                break;
            case 6:
                break;
            default:
        }
        requestResult.setObj(list);
        return requestResult;
    }
}
